package com.itheima.util;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Descriptionn:test
 * @Create by:依然
 * @Date:2021/11/4 17:00
 */
public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * 作用: 实现方法执行软植入
     *
     * @param request  域对象请求数据
     * @param response 响应数据
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取浏览器请求的执行方法名
        String methodName = request.getParameter("method");

        //获取Class对象
        Class Clazz = this.getClass();

        try {
            //获取web层使用的方法
            Method method = Clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //去除权限检查
            method.setAccessible(true);
            //传入参数执行方法

            method.invoke(Clazz.newInstance(), request, response);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
